﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace AutoInsurance
{
    public partial class policyData : System.Web.UI.Page
    {
        string strSqlConnection = "Data Source=localhost\\sqlexpress;Initial Catalog=Autoinsurance;Integrated Security=True";
        string vin_code;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                vin_code = Request.QueryString["vincode"];


                SqlConnection sqlCon = new SqlConnection(strSqlConnection);
                sqlCon.Open();
                string strQuery1 = "SELECT * FROM Policy where vin_code=" + "'" + vin_code + "'";
                string strQuery2 = "SELECT * FROM Policy where giveout=" + "'0'";
                DataSet ds1 = new DataSet();
                DataSet ds2 = new DataSet();
                SqlDataAdapter da1 = new SqlDataAdapter();
                SqlDataAdapter da2 = new SqlDataAdapter();
                da1.SelectCommand = new SqlCommand(strQuery1, sqlCon);
                da2.SelectCommand = new SqlCommand(strQuery2, sqlCon);
                da1.Fill(ds1, "Policy");
                da2.Fill(ds2, "Policy");
                GridView1.DataSource = ds1.Tables["Policy"];
                GridView2.DataSource = ds2.Tables["Policy"];
                this.DataBind();


                sqlCon.Close();
            }
            catch (Exception ex)
            {
                Server.Transfer("Error.aspx?error=" + ex.Message);
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                String id_policy1 = GridView1.SelectedRow.Cells[1].Text;
                string myUrl = "newInsuredAccident.aspx?id_policy=" + id_policy1;
                Response.Redirect(myUrl);
            }
            catch (Exception ex)
            {
                Server.Transfer("Error.aspx?error=" + ex.Message);
            }
        }

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                String id_policy2 = GridView2.SelectedRow.Cells[1].Text;
                SqlConnection connection = new SqlConnection(strSqlConnection);
                connection.Open();
                int giveout = 1;
                SqlCommand newInsuredAccident = new SqlCommand("update Policy set giveout=" + "'" + giveout + "'" + "where id_policy=" + "'" + id_policy2 + "'", connection);
                newInsuredAccident.ExecuteNonQuery();
                connection.Close();
                string myUrl = "policyData.aspx?vincode=" + vin_code;
                Response.Redirect(myUrl);
            }
            catch (Exception ex)
            {
                Server.Transfer("Error.aspx?error=" + ex.Message);
            }
        }
    }
}